package com.prs.crm.dao.sys.impl;

import java.util.Collection;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.prs.crm.dao.impl.BaseDaoImpl;
import com.prs.crm.dao.sys.ShortCutMenuDao;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.domain.sys.ShortCutMenu;

public class ShortCutMenuDaoImpl extends BaseDaoImpl<ShortCutMenu> implements ShortCutMenuDao{

	public int removeMenus(final Person  person)
	{
		return (Integer)this.getHibernateTemplate().execute(
			      new HibernateCallback() {   
						public Object doInHibernate(Session session) throws HibernateException {
							Query query = session.createQuery("delete from ShortCutMenu where recorder=?");
							query.setParameter(0, person);
							return query.executeUpdate();
				        }   
				 } );
	}
	
	@SuppressWarnings("unchecked")
	public Collection<ShortCutMenu> findMenus(final Person person)
	{
		return (List<ShortCutMenu>)this.getHibernateTemplate().execute(
			      new HibernateCallback() {   
						public Object doInHibernate(Session session) throws HibernateException {
						
					        	Criteria criteria = session.createCriteria(ShortCutMenu.class);
					        	criteria.add(Restrictions.eq("recorder",person));
					        	criteria.addOrder(Order.asc("id"));
					        	return criteria.list();
				        }   
				 } );
	}
}
